package com.loveofsoftware.fotolab;

import info.himanshug.www.picfun.Constants;
import info.himanshug.www.picfun.ImageView;

import java.io.File;

import com.bala.R;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SeekBar;

public class PicfunActivity extends Activity {

	private static final int SELECT_PICTURE = 1;
//	private static final int REQUEST_SAVE = 2;

	private String selectedImagePath;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        
        String imagePath = null;
        if(bundle != null) {
        	imagePath = bundle.getString(Constants.BUNDLE_IMAGE_PATH_KEY);
        }
        else {
        	Bundle extras = getIntent().getExtras();
        	imagePath = extras.getString("fileFromCamera");
        }
	    
        Display display = getWindowManager().getDefaultDisplay();
        int w = display.getWidth();
        int h = display.getHeight()-50;
        
		Log.d(Constants.APP_NAME,"setting static display width height in render thread to be.." + w + " and " + h);
        
        if(imagePath != null && imagePath.trim().length() > 0) {
        	selectedImagePath = imagePath;
        	Log.d(Constants.APP_NAME,"creating activity step1....");
 	        setContentView(R.layout.picfun_main);
 	        Log.d(Constants.APP_NAME,"creating activity step2..");
 	        ImageView fotoView = ((ImageView) findViewById(R.id.pic_view));
 	        Log.d(Constants.APP_NAME,"creating activity step3..");
 	        fotoView.setActivity(this);
 	        Log.d(Constants.APP_NAME,"creating activity step4..");
 			SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar_1);
 			Log.d(Constants.APP_NAME,"creating activity step5..");
 			String s = bundle != null ? bundle.getString(Constants.BUNDLE_SEEKBAR_VALUE_KEY) : null;
 			int previousSeekbarProgress = s != null && s.trim().length() > 0 ? Integer.parseInt(s) : -1;
 			if(previousSeekbarProgress > 0)
 				seekBar.setProgress(previousSeekbarProgress);
 			seekBar.setOnSeekBarChangeListener(fotoView);
 			Log.d(Constants.APP_NAME,"creating activity step6..");
        	Log.d(Constants.APP_NAME,"opening old foto....");
			Log.d(Constants.APP_NAME,"Fotoview Height is: " + fotoView.getHeight());
			Log.d(Constants.APP_NAME,"Fotoview Width is: " + fotoView.getWidth());
			fotoView.getRenderThread().setDisplayWidthHeight(w,h);
			fotoView.openImage(imagePath,previousSeekbarProgress);
			Log.d(Constants.APP_NAME,"opened foto........");
        }
        else {
	        //Log.e(Constants.APP_NAME,"Starting............");
	        Log.d(Constants.APP_NAME,"creating activity step1..");
	        setContentView(R.layout.picfun_main);
	        Log.d(Constants.APP_NAME,"creating activity step2..");
	        ImageView fotoView = ((ImageView) findViewById(R.id.pic_view));
	        Log.d(Constants.APP_NAME,"creating activity step3..");
	        fotoView.setActivity(this);
	        fotoView.getRenderThread().setDisplayWidthHeight(w,h);
	        Log.d(Constants.APP_NAME,"creating activity step4..");
			SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar_1);
			Log.d(Constants.APP_NAME,"creating activity step5..");
			seekBar.setOnSeekBarChangeListener(fotoView);
			Log.d(Constants.APP_NAME,"creating activity step6..");
			
			
			
			
	        //Log.e(Constants.APP_NAME,"Starting1..........");
	        //findViewById(R.id.outer_frame_layout).setOnTouchListener(this);
	        //Log.e(Constants.APP_NAME,"Starting2..........");
	
	//        findViewById(R.id.buttons_layout).setOnTouchListener(this);
			//create directory to save images if needed
			Log.d(Constants.APP_NAME,"creating activity step7..");
			File outputDir = new File(Constants.SAVED_IMAGE_FOLDER);
			if(!outputDir.exists())
				outputDir.mkdir();
			
			Log.d(Constants.APP_NAME,"creating activity step8...");
	        selectFile();
	        Log.d(Constants.APP_NAME,"creating activity step9...");
        }
    }
    
	private void selectFile() {
		Intent intent = new Intent();
		intent.setType("image/*");
		intent.setAction(Intent.ACTION_GET_CONTENT);
		startActivityForResult(Intent.createChooser(intent, "Select Picture"),
				SELECT_PICTURE);
	}
	
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				Log.d(Constants.APP_NAME,"opening foto....");
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
				ImageView fotoView = ((ImageView) findViewById(R.id.pic_view));
				fotoView.setActivity(this);
				Log.d(Constants.APP_NAME,"Fotoview Height is: " + fotoView.getHeight());
				Log.d(Constants.APP_NAME,"Fotoview Width is: " + fotoView.getWidth());
//				fotoView.getRenderThread().setDisplayWidthHeight(fotoView.getWidth(),fotoView.getHeight());
				fotoView.openImage(selectedImagePath,-1);
				Log.d(Constants.APP_NAME,"opened foto........");
			}
		}
	}
	
	private String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// return super.onCreateOptionsMenu(menu);
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.picfun_menu, menu);
		return true;

	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.open:
			selectFile();
			return true;
		case R.id.save:
			ImageView fotoView = ((ImageView) findViewById(R.id.pic_view));
			fotoView.save(false);
			rescanMedia();
			return true;
		case R.id.exit:
			this.finish();
			return true;
		case R.id.scanMedia:
			rescanMedia();
			return true;
		case R.id.share:
			fotoView = ((ImageView) findViewById(R.id.pic_view));
			fotoView.setActivity(this);
			fotoView.save(true);
			return true;
		default:
			return super.onContextItemSelected(item);
		}

	}
	
	private void rescanMedia() {
		sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
				.parse("file://" + Environment.getExternalStorageDirectory())));
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		if(selectedImagePath != null && selectedImagePath.trim().length() > 0)
			outState.putString(Constants.BUNDLE_IMAGE_PATH_KEY, selectedImagePath);
		
		SeekBar seekBar = (SeekBar)findViewById(R.id.seekbar_1);
		if(seekBar != null)
			outState.putString(Constants.BUNDLE_SEEKBAR_VALUE_KEY, Integer.toString(seekBar.getProgress()));
	}
	
//	private void toggleButtonsVisibility() {
//		LinearLayout ll = (LinearLayout)findViewById(R.id.buttons_layout);
//		int currVisibility = ll.getVisibility();
//		switch (currVisibility) {
//		case View.VISIBLE:
//			ll.setVisibility(View.INVISIBLE);
//		case View.INVISIBLE:
//			ll.setVisibility(View.VISIBLE);
//		default:
//			ll.setVisibility(View.VISIBLE);
//		}
//	}

//	private void showButtons() {
//		final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar);
//		dialog.setContentView(R.layout.buttons_layout);
//        if(dialog.findViewById(R.id.exit) == null)
//        	Log.e(Constants.APP_NAME,"Exit button view not found");
//        dialog.findViewById(R.id.exit).setOnClickListener(this);
//        Log.e(Constants.APP_NAME,"Starting3..........");
//        dialog.findViewById(R.id.save).setOnClickListener(this);
//        Log.e(Constants.APP_NAME,"Starting4..........");
//		dialog.show();
//	}
	
//	@Override
//	public boolean onTouch(View v, MotionEvent event) {
//		// TODO Auto-generated method stub
//		//toggleButtonsVisibility();
//		showButtons();
//		return true;
//	}

//	@Override
//	public void onClick(View v) {
//		int id = v.getId();
//		switch(id) {
//		case R.id.save: //save
//			ImageView fotoView = ((ImageView) findViewById(R.id.pic_view));
//			fotoView.save();
//			rescanMedia();
//			break;
//		case R.id.exit: //exit
//			this.finish();
//			break;
//		default:
//			Log.e(Constants.APP_NAME,"Unknown ID on click " + id);
//		}
//		
//	}
}